<?php

namespace Addons\QrCode\Controller;

use Addons\QrCode\Model\QrCodeInfoModel;
use Addons\QrCode\Model\QrCodeLogModel;
use Home\Controller\AddonsController;
use Think\Page;

class QrCodeController extends AddonsController
{
    protected $infoModel;
    protected $logModel;
    protected $base_url;

    public function __construct()
    {
        parent::__construct();
        $this->infoModel = new QrCodeInfoModel();
        $this->logModel = new QrCodeLogModel();
        $this->base_url = "QrCode/View/default/QrCode/Admin/";

        $url = _ACTION;
        if (!strcasecmp($url, 'useQrCode')){
            if (!is_login()) {
                echo $this->error('您还没有登录，请先登录！', U('User/login'));
            }
        }
        $nav = array();


        $res['title'] = '二维码列表';
        $res['url'] = addons_url('QrCode:/QrCode/config');
        $res['class'] = (strcasecmp($url, 'config') == 0) || (strcasecmp($url, 'cancel') == 0) ? 'current' : '';
        $nav[] = $res;

        $res['title'] = '二维码生成';
        $res['url'] = addons_url('QrCode:/QrCode/main');
        $res['class'] = (strcasecmp($url, 'main') == 0)   ? 'current' : '';
        $nav[] = $res;


        $this->assign('nav', $nav);

    }

    public function main()
    {
        $price = $_POST["price"];
        $vipNum = $_POST["vip_num"];
        $userId = $_SESSION['weiphp_home']['user_auth']["uid"];
//        $userId = null;
//        print_r($userId);
        if (!empty($price) && !empty($vipNum)) {
            $data = array(
                "price" => $price,
                "vip_num" => $vipNum,
                "status" => 1,
                "create_time" => date('Y-m-d H:i:s', time()),
                "create_user" => $userId
            );
            $result = $this->infoModel->data($data)->add();
            //生成二维码
            $codeValue = addons_url('QrCode:/QrCode/useQrCode?id=' . $result . "&price=" . $price . "&vipNum" . $vipNum);
            $filename = qrcode($codeValue);
            $path = "/Uploads/Picture/QRcode";
            $filename_path = $path . '/' . $filename; //合成路径
            $pcId = M("Picture")->data(array(
                "path"=>$filename_path,
                "status"=>1,
                "create_time"=>time()
                ))->add();
            $this->infoModel->data(array("qrcode_path"=>$pcId))->where("id=".$result)->save();
            $this->assign('qrcode', SITE_URL . $filename_path);

        }
        $this->assign('post_url', addons_url('QrCode:/QrCode/main'));
        $this->display(ONETHINK_ADDON_PATH . $this->base_url . 'main.html');
    }

    public function cancel(){
        $this->infoModel->data(array("status"=>3))->where(array("id"=>$_GET['action_id']))->save();
        $this->index();
    }

    private function index()
    {
        // 获取参数
        $rows = 10;
        $page = (I('p', 1, 'intval') - 1) * $rows;
        // 进行查询
        $data = $this->infoModel->order("id desc")->limit($page,$page+$rows)->select();
        $count = $this->infoModel->count();
        // 设置分页
        if ($count > $rows) {
            $page = new Page($count, $rows);
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }

        // 设置表头
        $fields = array();
        $fields[] = array('title' => '编号', 'name' => 'id', 'type' => 'num');
        $fields[] = array('title' => '金额', 'name' => 'price', 'type' => 'num');
        $fields[] = array('title' => 'VIP票数', 'name' => 'vip_num', 'type' => 'num');
        $fields[] = array('title' => '创建时间', 'name' => 'create_time', 'type' => 'datetime');
//        $fields[] = array('title' => '创建者', 'name' => 'create_user', 'type' => 'string');
        $fields[] = array('title' => '图片', 'name' => 'qrcode_path', 'type' => 'picture');

        // 设置参数
        $this->assign('list_fields', $fields);
        $this->assign('list_data', $data);
        $this->assign("page",$_GET['p']);


        $this->display(ONETHINK_ADDON_PATH . $this->base_url . 'list.html');
    }

    public function config()
    {
        $this->index();
    }

    public function useQrCode()
    {

        $id = $_GET['id'];
        $result = $this->infoModel->where("id=" . $id)->select();
        if (!empty($result) && $result[0]['status'] == 1) {
//            二维码验证成功
            $this->infoModel->data('status=2')->where("id=" . $result['id'])->save();
            $data = array(
                action_id => '',
                openid => '',
                vip_num => '',
                create_time => date('Y-m-d H:i:s', time()),
            );
            $this->logModel->data($data)->add();

            echo "success";
        } else
            echo "fail";

    }
}
